package com.shop.api;

import com.shop.po.R;
import com.shop.user.po.AddressBook;
import com.shop.user.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-22
 */
@RestController
@RequestMapping("address")
@CrossOrigin
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    @GetMapping("/getInfo")
    public R getAllAddress(){
        List<AddressBook> allAddress = addressBookService.getAllAddress();
        return R.ok("成功获取用户所有地址").data("list",allAddress);
    }

    /**
     * 添加用户收货地址
     * @param address
     */
    @PostMapping("/add")
    public R AddAddressBook(@RequestBody AddressBook address){
        addressBookService.addAddressBook(address);
        return R.ok("成功添加用户地址");
    }

    /**
     * 删除用户该地址
     * @param id
     */
    @PostMapping("/delete")
    public R deleteAddress(Long id){
        addressBookService.deleteAddress(id);
        return R.ok("成功删除用户地址");
    }



    /**
     * 将该id设置为默认地址
     * @param id
     */
    @PostMapping("/setDefault")
    public R setDefaultAdd(Long id){
        addressBookService.setDefaultAdd(id);
        return R.ok("成功设置默认地址");
    }

    /**
     * 编辑收货地址:这里不修改“默认地址”！
     * 只修改地址，姓名，电话，标签
     * @param addressBook
     */
    @PostMapping("/edit")
    public R editAddress(@RequestBody AddressBook addressBook){
        addressBookService.editAddress(addressBook);
        return R.ok("成功修改地址");
    }




}

